﻿@page "/changepassword"

@using Radzen
@using Radzen.Blazor
@inject NotificationService NotificationService
@inject DialogService DialogService

<RadzenCard Style="max-width: 500px; margin: auto;">
    <h3 style="text-align: center;">修改密码</h3>
    
    <RadzenTemplateForm Data="@model" Style="margin-top: 20px;">
        <div class="row">
            <div class="col-md-12">
                <RadzenPassword Name="currentPassword" Placeholder="当前密码" @bind-Value="@model.CurrentPassword" 
                               Style="width: 100%;" Required />
                <RadzenRequiredValidator Component="currentPassword" Text="请输入当前密码" Style="position: absolute" />
            </div>
        </div>
        
        <div class="row" style="margin-top: 15px;">
            <div class="col-md-12">
                <RadzenPassword Name="newPassword" Placeholder="新密码" @bind-Value="@model.NewPassword" 
                               Style="width: 100%;" Required />
                <RadzenRequiredValidator Component="newPassword" Text="请输入新密码" Style="position: absolute" />
                <RadzenPasswordStrength Component="newPassword" MinimumLength="8" 
                                      RequireDigit="true" RequireLowercase="true" 
                                      RequireUppercase="true" RequireNonAlphanumeric="true" />
            </div>
        </div>
        
        <div class="row" style="margin-top: 15px;">
            <div class="col-md-12">
                <RadzenPassword Name="confirmPassword" Placeholder="确认新密码" @bind-Value="@model.ConfirmPassword" 
                               Style="width: 100%;" Required />
                <RadzenRequiredValidator Component="confirmPassword" Text="请确认新密码" Style="position: absolute" />
                <RadzenCompareValidator Component="confirmPassword" Text="两次输入的密码不一致" 
                                      Value="@model.NewPassword" Style="position: absolute" />
            </div>
        </div>
        
        <div class="row" style="margin-top: 25px;">
            <div class="col-md-6">
                <RadzenButton ButtonType="ButtonType.Submit" Text="提交" ButtonStyle="ButtonStyle.Primary" 
                            Icon="save" Style="width: 100%;" Disabled="@isLoading" />
            </div>
            <div class="col-md-6">
                <RadzenButton ButtonType="ButtonType.Button" Text="取消" ButtonStyle="ButtonStyle.Secondary" 
                            Icon="cancel" Style="width: 100%;" @onclick="OnCancel" />
            </div>
        </div>
    </RadzenTemplateForm>
</RadzenCard>

@code {
    private ChangePasswordModel model = new ChangePasswordModel();
    private bool isLoading;
    private string userId;

    protected override async Task OnInitializedAsync()
    {
        
    }

    private async Task OnSubmit()
    {
        isLoading = true;
        
        try
        {
            // 验证新密码和确认密码是否一致
            if (model.NewPassword != model.ConfirmPassword)
            {
                NotificationService.Notify(new NotificationMessage
                {
                    Severity = NotificationSeverity.Error,
                    Summary = "错误",
                    Detail = "新密码和确认密码不匹配",
                    Duration = 4000
                });
                return;
            }

            // 调用API修改密码
            var result = await ChangePasswordApi(model);
            
            if (result.Success)
            {
                NotificationService.Notify(new NotificationMessage
                {
                    Severity = NotificationSeverity.Success,
                    Summary = "成功",
                    Detail = "密码修改成功",
                    Duration = 4000
                });
                
                // 关闭对话框（如果是在对话框中打开的）
                DialogService.Close(true);
            }
            else
            {
                NotificationService.Notify(new NotificationMessage
                {
                    Severity = NotificationSeverity.Error,
                    Summary = "错误",
                    Detail = result.Message,
                    Duration = 4000
                });
            }
        }
        catch (Exception ex)
        {
            NotificationService.Notify(new NotificationMessage
            {
                Severity = NotificationSeverity.Error,
                Summary = "错误",
                Detail = $"修改密码时发生错误: {ex.Message}",
                Duration = 4000
            });
        }
        finally
        {
            isLoading = false;
        }
    }

    private void OnCancel()
    {
        DialogService.Close(false);
    }

    // 模拟API调用
    private async Task<ApiResult> ChangePasswordApi(ChangePasswordModel model)
    {
        // 这里应该是实际的API调用
        await Task.Delay(1000); // 模拟网络延迟
        
        // 模拟成功响应
        return new ApiResult { Success = true, Message = "密码修改成功" };
    }

    public class ChangePasswordModel
    {
        public string CurrentPassword { get; set; }
        public string NewPassword { get; set; }
        public string ConfirmPassword { get; set; }
    }

    public class ApiResult
    {
        public bool Success { get; set; }
        public string Message { get; set; }
    }
}
